package com.sl.crawler.robot.downloader;

import com.sl.crawler.robot.clientPool.BaseClientPool;
import com.sl.crawler.robot.request.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by shiling on 2017/6/8.
 */
public interface BaseDownloader<T> {
    Logger LOG = LoggerFactory.getLogger(BaseDownloader.class);

    /**
     * 下载数据
     *
     * @param request
     * @return
     * @throws Exception
     */
    default String download(Request request) throws Exception {
        String pageSource = null;
        T webClient = null;
        try {
            webClient = getBaseClientPool().getClient();
            pageSource = getBaseClientPool().getUrl(webClient, request);
        } catch (Exception e) {
            LOG.error("错误原因=" + e.toString());
        } finally {
            getBaseClientPool().returnToPool(webClient);
        }
        return pageSource;
    }

    BaseClientPool<T> getBaseClientPool();
}
